package com.lanou.musicbox.fragment.seekmusic;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.musicbox.R;
import com.lanou.musicbox.bean.AlbumMessageEntity;
import com.lanou.musicbox.utils.Contant;
import com.lanou.musicbox.utils.VolleySingleton;
import com.squareup.picasso.Picasso;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * Created by 赵宝龙 on 15/10/23.
 * 专辑内容界面
 */
public class Enterthe_Album_Fragment extends Fragment implements View.OnClickListener {

    @Bind(R.id.album_message_back)
    ImageView albumMessageBack;
    @Bind(R.id.album_message_title)
    TextView albumMessageTitle;
    @Bind(R.id.album_searching)
    ImageView albumSearching;
    @Bind(R.id.album_big_img)
    ImageView albumBigImg;
    @Bind(R.id.album_message)
    TextView albumMessage;
    @Bind(R.id.al_name)
    TextView alName;
    @Bind(R.id.album_name)
    TextView albumName;
    @Bind(R.id.publish_time_text)
    TextView publishTimeText;
    @Bind(R.id.publish_time)
    TextView publishTime;
    @Bind(R.id.album_type_text)
    TextView albumTypeText;
    @Bind(R.id.album_type)
    TextView albumType;
    @Bind(R.id.album_language_text)
    TextView albumLanguageText;
    @Bind(R.id.album_language)
    TextView albumLanguage;
    @Bind(R.id.album_brief)
    TextView albumBrief;
    private String url;
    private AlbumMessageEntity.DataEntity data;
    private RequestQueue requestQueue;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.enterthe_album_list, null);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        requestQueue = VolleySingleton.getInstance(getActivity()).getRequestQueue();
        albumMessageBack.setOnClickListener(this);
        albumSearching.setOnClickListener(this);
        Bundle bundle = getArguments();
        int id = bundle.getInt("id");
        url = Contant.Url.URL_Album_info_first + id + Contant.Url.URL_Album_info_end;

        getInternetData();
    }


    //从网络上拉取数据
    private void getInternetData() {
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Gson gson = new Gson();
                AlbumMessageEntity entity = gson.fromJson(s, AlbumMessageEntity.class);
                if (entity != null && entity.getData() != null) {
                    data = entity.getData();
                    setData();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });
        requestQueue.add(request);
    }

    //向页面设置数据
    private void setData() {
        albumMessageTitle.setText(data.getName());
        albumName.setText(data.getName());
        publishTime.setText(data.getPublishDate());
        albumType.setText(data.getTypeName());
        albumLanguage.setText(data.getLang());
        albumBrief.setText(data.getDescription());
        Picasso.with(getActivity()).load(data.getPicUrl()).into(albumBigImg);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.album_searching:
                getFragmentManager()
                        .beginTransaction()
                        .addToBackStack(null)
                        .replace(R.id.fill_main, new SeekMusicsFragment())
                        .commit();
                break;
            case R.id.album_message_back:
                getFragmentManager().popBackStack();
                break;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
        getFragmentManager()
                .beginTransaction()
                .remove(Enterthe_Album_Fragment.this)
                .commit();
    }
}
